package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ShoppingCartServiceImp extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    /*
    * 购物车订单添加
    * */
    @Override
    public ShoppingCart myAdd(ShoppingCart shoppingCart) {

        //先添加订单人的userid
        long userid = 10018;

        shoppingCart.setUserId(userid);

        //判断为添加的菜品还是添加的套餐
        Long dishId = shoppingCart.getDishId();

        if (dishId != null){
            //添加关于dish相关的数据，先判断原来数据中是否添加过相关的菜品如果添加过再次添加的时候将改变number字段的值
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
            ShoppingCart shoppingCart1 = this.getOne(queryWrapper);

            if (shoppingCart1 != null){
                Integer number = shoppingCart1.getNumber();
                shoppingCart1.setNumber(number + 1);
                this.updateById(shoppingCart1);

                return shoppingCart1;
            }else {
                this.save(shoppingCart);

                return shoppingCart;
            }

        }else {
            //套餐添加
            LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            lambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
            ShoppingCart shoppingCart2 = this.getOne(lambdaQueryWrapper);

            if (shoppingCart2 != null){
                Integer number = shoppingCart2.getNumber();
                shoppingCart2.setNumber(number + 1);
                this.updateById(shoppingCart2);

                return shoppingCart2;
            }else {

                this.save(shoppingCart);
                return shoppingCart;

            }

        }

    }

    /*
    * 减少菜品或套餐数据
    * */
    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {

        //用户id
        long userid = 10018;


        //判断是菜品id还是套餐id
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            //是菜品id
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
            queryWrapper.eq(ShoppingCart::getUserId,userid);
            ShoppingCart shoppingCart1 = this.getOne(queryWrapper);
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number - 1);
            this.updateById(shoppingCart1);
            return shoppingCart1;

        }else {
            LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            queryWrapper1.eq(ShoppingCart::getUserId,userid);
            ShoppingCart shoppingCart1 = this.getOne(queryWrapper1);
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number - 1);
            this.updateById(shoppingCart1);
            return shoppingCart1;

        }


    }
}
